Skip to content

Kafka-clients: records(TopicPartition): unit tests + some (potential) improvements#924

Merged
tylerbenson merged 4 commits into
DataDog:masterfrom
gygabyte:master
Jul 19, 2019
Merged

Kafka-clients: records(TopicPartition): unit tests + some (potential) improvements#924
tylerbenson merged 4 commits into
DataDog:masterfrom
gygabyte:master

Conversation

@gygabyte
Copy link
Copy Markdown
Contributor

@gygabyte gygabyte commented Jul 18, 2019

I think these instrumented methods should safeguard for misuse and therefore prevent inaccurate traces. Since these methods can only be instrumented when applications calls them as it is imposed by the synchronous nature of the Kafka-client, then we should ensure (up to certain point) the agent collects what exactly the Kafka-client has processed. One example is, for instance, the application calls the iterator multiple times.
subList should not be instrumented as the application can remove elements of the list (as the method implies) and therefore, again, wrong traces can be collected which won't be the real representation of what has been received by the client

gygabyte and others added 3 commits July 13, 2019 11:14
…mentation. introduce safeguards when instrumenting iterator() so that duplication of traces is not allowed. do not allow subList() to be instrumented
@gygabyte gygabyte requested a review from a team as a code owner July 18, 2019 22:20
Copy link
Copy Markdown
Contributor

@mar-kolya mar-kolya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you so much for working on this!

Just couple of minor changes requested.
Thanks!

@Override
public Iterator<ConsumerRecord> iterator() {
return super.iterator();
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor note: I think you can drop this method from here - it will be inherited from iterable

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep, I understand, but I was getting compiler warnings regarding @NotNull annotation.. not a big deal ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No. you can leave this as is if you prefer

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will take it out, we can avoid a call in the stack. thanks!

cleanup:
producerFactory.stop()
container?.stop()
embeddedKafka.after()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like you could make kafka rule use @Rule instead of @Shared @ClassRule and then you would not need to manually call before/after on it

producer.send(new ProducerRecord<Integer, String>(SHARED_TOPIC, kafkaPartition, null, greeting))
TEST_WRITER.waitForTraces(1)

then:
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like you may be able to simplify this to something like:

    when:
    def greeting = "Hello from MockConsumer!"
    producer.send(new ProducerRecord<Integer, String>(SHARED_TOPIC, kafkaPartition, null, greeting))

    then:
    TEST_WRITER.waitForTraces(1) // ensure consistent ordering of traces
    def pollResult = KafkaTestUtils.getRecords(consumer)
    def records = pollResult.records(new TopicPartition(SHARED_TOPIC, kafkaPartition)).iterator()

    def first
    while (records.hasNext()) {
      first = records.next()
      break
    }
    records.hasNext() == false
    first.value() == greeting
    first.key() == null

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I will change the WAIT_TRACES of producer to the then block:

On other note, I don't need to wait for traces when consuming?

Copy link
Copy Markdown
Contributor

@mar-kolya mar-kolya Jul 19, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assertTraces(2) { waits to see exactly two traces. If I understand things correctly you need TEST_WRITER.waitForTraces(1) only once right before consuming - to make sure producer traces have been shipped - so two traces do not appear out of order at assertion time.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep makes sense. I have updated the PR as per your comments

Comment thread LICENSE-3rdparty.csv Outdated
import (test),org.junit,EPL-1.0,Copyright © 2002-2017 JUnit. All Rights Reserved.
import (test),org.assertj,Apache-2.0,Copyright 2012-2017 the original author or authors.
import (test),org.mockito,MIT,Copyright (c) 2007 Mockito contributors
kafka-clients: method records(TopicPartition) partial+test,"Aspect Software, Inc.", Apache-2.0, "Copyright (C) Aspect Software, Inc."
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I do not think this change is needed. I think this file is specifically for external deps that we ship with the built agent. This change doesn't add new dependencies.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

sure, this is more about the contribution as I/we thought it was what this is file is for. Is there anyway of the contribution being recognised in some other place? Thanks!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We will certainly give you a shout-out in our release notes. Thanks again for taking the time and effort to contribute.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Appreciate it! Happy to contribute!

… refactor unit tests to expect only one element to be consumed. Kafka embedded instance as a Rule
Copy link
Copy Markdown
Contributor

@mar-kolya mar-kolya left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, Thanks!

@tylerbenson tylerbenson added tag: community Community contribution inst: others All other instrumentations labels Jul 19, 2019
@tylerbenson tylerbenson merged commit ace9b53 into DataDog:master Jul 19, 2019
@tylerbenson tylerbenson added this to the 0.31.0 milestone Jul 25, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

inst: others All other instrumentations tag: community Community contribution

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants